<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
class Plugin_todo implements Plugin {
	public static function getName() {
		return 'The ToDo list';
	}
	public static function getVersion() {
		return '1.1';
	}
	public static function getDescription() {
		return __('Модуль дозволяє керувати списком завдань (ToDo) на сайті');
	}
	public static function getHasAdmin() {
		return false;
	}
	public static function run() {
		if (AJAX) {
			if (!Users::getInstance()->is_admin()) return;
			if (isset($_POST['todoedit']) && $_POST['todoedit'] == 'yes') {
				$data = Parser::getInstance()->unParseBB(FileDB::select('todo', 'plugin'));
				$brcount = substr_count($data,N) + 1;
				$brcount = $brcount<5 ? 5 : $brcount;
				Aurus::getInstance()->addContent( Aurus::getInstance()->getEditor() );
				Aurus::getInstance()->addContent( '<textarea id="todotarea" style="width:100%;" rows="'. $brcount .'">' . $data . '</textarea><br/><a href="javascript:void(0)" onclick="au_ajax(false,\'todosave=yes&todo=\' + $(\'#todotarea\').val(),\'todo\',1,1)">' . __('Зберегти дані') . '</a>');
				Filter::setFilter('Aurus::getContent',array(__CLASS__,'filter'));
			}
			if (isset($_POST['todosave']) && $_POST['todosave'] == 'yes') {
				FileDB::insert('todo', 'plugin', Parser::getInstance()->parse($_POST['todo']));
				echo '<div id="todo">' . FileDB::select('todo', 'plugin') . '</div>';
			}
		} else {
			if (Users::getInstance()->is_admin()) {
				$todo = '<div id="todo">' . FileDB::select('todo', 'plugin') . '</div>';
				$todo .= '<br/><a href="javascript:void(0)" onclick="au_ajax(false,\'todoedit=yes\',\'todo\',1,1)">' . __('Оновити дані') . '</a>';
			} else {
				$todo = FileDB::select('todo', 'plugin');
			}
			Template::getInstance()->set('{todo}', $todo, 'main');
		}
	}
	public static function filter($data){
		return str_replace('{THEME}', LINK_ROOT . 'templates/' . Aurus::getInstance()->getTemplateToLoad(),$data);
	}
	public static function onActivate(){}
	public static function onDeactivate(){}
	public static function initAdminArea(){}
}